﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Walson.Windows.Forms
{
    public partial class Switch : UserControl
    {
        /// <summary>
        /// 开关控件的风格
        /// </summary>
        public enum SwitchStyles
        {
            /// <summary>
            /// 风格1
            /// </summary>
            Style1,
            /// <summary>
            /// 风格2
            /// </summary>
            Style2
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler Click;
        protected override void OnClick(EventArgs e)
        {
            if (this.Click != null)
            {
                Click(this, e);
            }
        }

        /// <summary>
        /// 开关状态改变
        /// </summary>
        public event EventHandler StateChanged;
        protected void RaiseStateChanged()
        {
            if (this.StateChanged != null)
            {
                StateChanged(this, null);
            }
        }

        private SwitchStyles _Style = SwitchStyles.Style1;
        /// <summary>
        /// 开关的风格
        /// </summary>
        public SwitchStyles Style
        {
            get
            {
                return _Style;
            }
            set
            {
                if(_Style!=value)
                {
                    _Style = value;
                    if (this.On)
                    {
                        if (_Style == SwitchStyles.Style1)
                        {
                            pictureBox1.BackgroundImage =Walson.Windows.Forms.Properties.Resources.SwitchOn1;
                        }
                        else
                        {
                            pictureBox1.BackgroundImage = Walson.Windows.Forms.Properties.Resources.SwitchOn2;
                        }
                    }
                    else
                    {
                        if (this.Style == SwitchStyles.Style1)
                        {
                            pictureBox1.BackgroundImage = Walson.Windows.Forms.Properties.Resources.SwitchOff1;
                        }
                        else
                        {
                            pictureBox1.BackgroundImage = Walson.Windows.Forms.Properties.Resources.SwitchOff2;
                        }
                    }
                }
            }
        }

        private bool _On = true;
        /// <summary>
        /// 开关的状态
        /// </summary>
        public bool On
        {
            get
            {
                return _On;
            }
            set
            {
                if (_On != value)
                {
                    _On = value;
                    RaiseStateChanged();
                    if (_On)
                    {
                        if (this.Style == SwitchStyles.Style1)
                        {
                            pictureBox1.BackgroundImage = Walson.Windows.Forms.Properties.Resources.SwitchOn1;
                        }
                        else
                        {
                            pictureBox1.BackgroundImage = Walson.Windows.Forms.Properties.Resources.SwitchOn2;
                        }
                    }
                    else
                    {
                        if (this.Style == SwitchStyles.Style1)
                        {
                            pictureBox1.BackgroundImage = Walson.Windows.Forms.Properties.Resources.SwitchOff1;
                        }
                        else
                        {
                            pictureBox1.BackgroundImage = Walson.Windows.Forms.Properties.Resources.SwitchOff2;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 开关
        /// </summary>
        public Switch()
        {
            InitializeComponent();
            pictureBox1.BackgroundImage = Walson.Windows.Forms.Properties.Resources.SwitchOff1;

            pictureBox1.Click += delegate
            {
                this.On = !this.On;
            };
        }
    }
}
